<?php
/**
 * 表单生成
 */
namespace app\common\controller;

use think\Controller;

class FormBuilder extends Controller
{
    private $_form_items = [];  //表单项目
    private $_form_data   = []; // 表单数据
    private $_model = '';       //操作model
    private $_success_url = ''; //操作成功url
    private $_url = ''; //数据操作url
    private $_extra_html = '';  //额外的html代码

    //初始化
    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * 加入一个表单项
     * @param string $name 表单名
     * @param string $type 表单类型
     * @param string $title 表单标题
     * @param string $remark 说明
     * @param array $options 表单options
     * @param int $is_require 是否必填
     * @param string $extra_class 表单项是否隐藏
     * @param int $checkall 是否全选，对checkbox起作用
     * @return $this
     */
    public function addFormItem($name = '', $type = '', $title = '', $remark = '', $options = [], $is_require = 0, $extra_class = '', $checkall = 0, $max_length = 0){
        $item['name'] = $name;
        $item['type'] = $type;
        $item['title'] = $title;
        $item['remark'] = $remark;
        $item['options'] = $options;
        $item['is_require'] = $is_require;
        $item['extra_class'] = $extra_class;
        $item['checkall'] = $checkall;
        $item['max_length'] = $max_length;
        $this->_form_items[] = $item;
        return $this;
    }

    /**
     * 设置表单表单数据
     * @param $form_data 表单数据
     * @return $this
     *
     */
    public function setFormData($form_data)
    {
        $this->_form_data = $form_data;
        return $this;
    }

    /**
     * 设置操作model
     * @param string $model
     * @return FormBuilder
     */
    public function setModel($model = '')
    {
        $this->_model = $model;
        return $this;
    }

    /**
     * 设置操作成功url
     * @param string $url
     * @return FormBuilder
     */
    public function setSuccessUrl($url = 'index', $params = [])
    {
        $this->_success_url = url($url, $params);
        return $this;
    }

    /**
     * 设置数据操作url
     * @param string $url
     * @return FormBuilder
     */
    public function setUrl($url = '')
    {
        $this->_url = url($url);
        return $this;
    }

    /**
     * 设置额外的html代码
     * @param string $extra_html    //额外的html代码
     * @return FormBuilder
     */
    public function setExtraHtml($extra_html = '')
    {
        $this->_extra_html = $extra_html;
        return $this;
    }

    public function returnForm()
    {
        // 编译表单值
        if (isset($this->_form_data)) {
            foreach ($this->_form_items as &$item) {
                if (isset($this->_form_data[$item['name']]) && !isset($item['value'])) {
                    $item['value'] = $this->_form_data[$item['name']];
                } else {
                    $item['value'] = '';
                }
            }
        }

        $this->assign('formList', $this->_form_items);
        //赋值删除等操作model
        $this->assign('model', $this->_model);
        $this->assign('success_url', $this->_success_url);
        $this->assign('url', $this->_url ? $this->_url : url('admin/Common/add_edit'));
        $this->assign('extra_html', $this->_extra_html);
    }
}
